import web
import mimetypes
import printcontent

urls = (
            '/file/(.+)', 'FileHandler',
        )

app = web.application(urls, globals())

class FileHandler:
    def GET(self, id):
        f = printcontent._File.get_file_by_id(id)
        if(f):
            contenttype = mimetypes.guess_type(f.filename)[0] or 'application/octet-stream' 
            web.header('Content-Type', contenttype)
            web.header('Content-Disposition', 'filename=%s' % f.filename)
            return f.content
        else:
            raise web.NotFound('File Not Found!')
        
if __name__ == '__main__':
    app.cgirun()
